package com.xj.qq.sliding_menu;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.xj.qq.R;
import com.xj.qq.adapters.MenuListViewAdapter;
import com.xj.qq.fragments.ConversationFragment;

/**
 * Created by XJ on 2015/12/26
 */
public class SlidingMenu extends HorizontalScrollView {

    public static int FLAG = 0;
    /**
     *屏幕宽度
     */
    private int mScreeWidth;
    /**
     * dp*/
    private int mMenuRightPadding = 60;
    /**
     * 菜单宽度*/
    private int mMenuWidth;
    private int mHalfMenuWidth;

    private boolean once;
    private boolean isOpen;

    private ListView ls;

    private DisplayMetrics dm;
    private ViewGroup content;
    private Context context;

    private int[] imgStrs = new int[]{R.drawable.qq_setting_svip,R.drawable.qq_setting_qianbao,
                                        R.drawable.qq_setting_zhuangban,R.drawable.qq_setting_shoucang,
                                        R.drawable.qq_setting_xiangce,R.drawable.qq_setting_wenjian};
    private String[] textStrs = new String[]{"我的QQ会员","QQ钱包","个性装扮","我的收藏","我的相册","我的文件"};

    public SlidingMenu(Context context) {
        super(context);
    }

    public SlidingMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        WindowManager wm = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
        dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,mMenuRightPadding,dm);
        mScreeWidth = dm.widthPixels;
        mMenuWidth = mScreeWidth - mMenuRightPadding;
        mHalfMenuWidth = mMenuWidth / 2;

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        /**
         * 显示的设置一个宽度*/
        if (!once){
            LinearLayout wrapper = (LinearLayout) getChildAt(0);
            ViewGroup menu = (ViewGroup) wrapper.getChildAt(0);
            content = (ViewGroup) wrapper.getChildAt(1);

            menu.getLayoutParams().width = mMenuWidth;
            content.getLayoutParams().width = mScreeWidth;

            ls = (ListView) menu.findViewById(R.id.ls_menu);
            MenuListViewAdapter mlva = new MenuListViewAdapter(imgStrs,textStrs,context);
            ls.setAdapter(mlva);
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed){
            this.smoothScrollTo(mMenuWidth, 0);
            once = true;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        int action = ev.getAction();
        switch (action){

            case MotionEvent.ACTION_UP:
                int scrollX = getScrollX();
                if (scrollX > mHalfMenuWidth/2) {
                    this.smoothScrollTo(mMenuWidth, 0);
                    isOpen = false;
                }
                else {
                    this.smoothScrollTo(0, 0);
                    isOpen = true;
                }
                return true;
        }
        return super.onTouchEvent(ev);
    }

    public void close(){
        if (!isOpen)
            return;
        this.smoothScrollTo(mMenuWidth,0);
        isOpen = false;
    }
    public void open(){
        if (isOpen)
            return;
        this.smoothScrollTo(0, 0);
        isOpen = true;
    }

    public void toggle(){
        if (isOpen){
            close();
        }else {
            open();
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ConversationFragment.FLAG == -1)
            return super.onInterceptTouchEvent(ev);
        else
            return false;
    }
}
